
exports.make = function(player, req, resp, onHandled) {
    do {
        var useCash = req.args.cash ? 1 : 0;

        var user = player.user;
        var huatuo = user.huatuo;

        if (user.status.level < gConfGlobal.HuatuoOpenLevel) {
            resp.code = 1; resp.desc = 'not open'; break;
        }

        if( huatuo.pellet ) {
            resp.code = 1; resp.desc = 'making'; break;
        }
        
        var haveTime = true;
        if( useCash ) {
            if( !gConfHuaTuoCost[huatuo.cash+1] ) {
                haveTime = false;
            }
        }else{
            var maxTimes = gConfGlobal.HuatuoDailyLimit;
            if( user.status.vip ) {
                maxTimes += gConfVip[user.status.vip].HuatuoTimes;
            }
            if( huatuo.free >= maxTimes ) {
                haveTime = false;
            }
        }

        
        if( !haveTime ) {
            resp.code = 1; resp.desc = 'not times'; break;
        }
        
        var conf = player.getHuaTuoLevelConf();
        var weights = {};
        for( var i=1; i<=4; i++ ) {
            weights[i] = conf['Weight'+i];
        }
        
        var pellet = +common.wRand(weights);
        
        var cashCost = 0;
        if( useCash ) {
            cashCost = gConfHuaTuoCost[huatuo.cash+1].Cash;
            if( !player.costResource('cash', cashCost, huatuo.cash) ) {
                resp.code = 1; resp.desc = 'not enough cash'; break;
            }
            huatuo.cash += 1;
        }else{
            huatuo.free += 1;
        }

        huatuo.pellet = pellet;
        huatuo.xp += 1;
        huatuo.quantity = 1;
        player.markDirty('huatuo');

        resp.data.huatuo = huatuo;
        resp.data.cash = -cashCost;

    }while(false);

    onHandled();
}

exports.promote = function(player, req, resp, onHandled) {
    do {
        var user = player.user;
        var huatuo = user.huatuo;

        if (user.status.level < gConfGlobal.HuatuoOpenLevel) {
            resp.code = 1; resp.desc = 'not open'; break;
        }

        if( !huatuo.pellet ) {
            resp.code = 1; resp.desc = 'not making'; break;
        }

        if( !gConfHuaTuoQuantity[huatuo.quantity+1] ) {
            resp.code = 1; resp.desc = 'up to max'; break;
        }

        var huatuoQuantityConf = gConfHuaTuoQuantity[huatuo.quantity];
        var cashCost = huatuoQuantityConf.Cash;
        if( !player.costResource('cash', cashCost, huatuo.quantity) ) {
            resp.code = 1; resp.desc = 'not enough cash'; break;
        }

        var rate = huatuoQuantityConf.Rate * ( 100 + player.getHuaTuoLevelConf().AddRate);
        if( common.randRange(1, 10000) <= rate ) {
            // 成功    
            huatuo.quantity += 1;
        }

        huatuo.score += cashCost;
        player.markDirty('huatuo');

        resp.data.cash = -cashCost;
        resp.data.huatuo = huatuo;
    }while(false);

    onHandled();
}

exports.exchange = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.id) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var scoreConf = gConfHuaTuoScore[req.args.id];
        if( !scoreConf ) {
            resp.code = 1; resp.desc = 'invalid id'; break;
        }

        var user = player.user;
        var huatuo = user.huatuo;

        if (user.status.level < gConfGlobal.HuatuoOpenLevel) {
            resp.code = 1; resp.desc = 'not open'; break;
        }

        if( huatuo.score < scoreConf.Score ) {
            resp.code = 1; resp.desc = 'not enough score'; break;
        }

        huatuo.score -= scoreConf.Score;
        player.markDirty('huatuo.score');

        var awards = [scoreConf.Award1];
        awards = player.addAwards(awards);

        resp.data.awards = awards;
        resp.data.score = -scoreConf.Score;

    }while(false);

    onHandled();
}

exports.harvest = function(player, req, resp, onHandled) {
    do {
        var user = player.user;
        var huatuo = user.huatuo;

        if (user.status.level < gConfGlobal.HuatuoOpenLevel) {
            resp.code = 1; resp.desc = 'not open'; break;
        }

        if( !huatuo.pellet ) {
            resp.code = 1; resp.desc = 'not making'; break;
        }

        var pelletConf = gConfHuaTuoPellet[huatuo.pellet][huatuo.quantity];
        var awards = [pelletConf.Award1, pelletConf.Award2, pelletConf.Award3, pelletConf.Award4];
        awards = player.addAwards(awards);

        huatuo.pellet = 0;
        huatuo.quantity = 0;
        player.markDirty('huatuo');

        resp.data.awards = awards;
        resp.data.huatuo = huatuo;

    }while(false);

    onHandled();
}
